//
//  MainViewController.m
//  WeiBo
//
//  Created by 小小罗 on 15/11/22.
//  Copyright © 2015年 RealMadrid. All rights reserved.
//


#import "MainViewController.h"

@interface MainViewController ()
@property(nonatomic,strong) UIButton*composedButton;
@end

@implementation MainViewController
#pragma mark  ****************** 懒加载 ******************
- (UIButton *)composedButton
{
    if (_composedButton == nil) {
        
        _composedButton = [UIButton initWithImageName:@"tabbar_compose_icon_add" backGroundImageName:@"tabbar_compose_button"];
        
    }
    
    return _composedButton;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tabBar.tintColor = [UIColor orangeColor];
    
    [self setUpAllChildVc];

    //自定义加载加号按钮
    [self setUpComposedButton];
}

- (void)viewDidAppear:(BOOL)animated{

    [super viewWillAppear:animated];
    
    //将加号按钮弄到最前面    (可能循环引用)
    [self.tabBar bringSubviewToFront:self.composedButton];


}


#pragma mark  ****************** 加号按钮 ******************
- (void) setUpComposedButton {
    
    [self.tabBar addSubview:self.composedButton];
    
    //设置按钮的frame
    CGFloat count = [self childViewControllers].count;
    
    //-1 表示让按钮宽一点点，能够解决手指触摸的容错问题
    int w = self.tabBar.frame.size.width / count - 1;
    self.composedButton.frame = CGRectInset(self.tabBar.bounds, 2 * w, 0);
    
    //给加号按钮添加单击事件
    [self.composedButton addTarget:self action:@selector(clickComposedButton) forControlEvents:UIControlEventTouchUpInside];
    
}

//加号按钮点击事件
- (void)clickComposedButton{
 
    NSLog(@"点我了!");
}

#pragma mark  ****************** 添加子控制器 ******************
- (void)setUpAllChildVc {

    //首页
    [self addChildVc:[[HomeViewController alloc] init] imageName:@"tabbar_home" title:@"首页"];
    //消息
    [self addChildVc:[[MessageViewController alloc] init] imageName:@"tabbar_message_center" title:@"消息"];
    //加号按钮
    [self addChildViewController:[[UIViewController alloc] init]];
    
    //发现
    [self addChildVc:[[DiscoverViewController alloc] init] imageName:@"tabbar_discover" title:@"发现"];
    //我
    [self addChildVc:[[MineViewController alloc] init] imageName:@"tabbar_profile" title:@"我"];
}


- (void)addChildVc:(UIViewController *)vc imageName:(NSString *)imageName title:(NSString *)title{
    
    vc.title = title;
    vc.tabBarItem.image = [UIImage imageNamed:imageName];
    
    //创建导航控制器
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    
    [self addChildViewController:nav];
    

}

@end
